package org.wichit.core.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.wichit.core.properties.WechatProperties;
import org.wichit.core.response.TokenResponse;
import org.wichit.core.response.UserResponse;

@Component
public class RestHttpService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WechatProperties wechatProperties;

    public UserResponse login(String jsCode) {
        return restTemplate.getForObject(wechatProperties.getLoginUrl() + "?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code"
                , UserResponse.class, wechatProperties.getAppId(), wechatProperties.getAppSecret(), wechatProperties, jsCode);
    }

    public TokenResponse getToken() {
        return restTemplate.getForObject(wechatProperties.getTokenUrl() + "?grant_type=client_credential&appid={0}&secret={1}"
                , TokenResponse.class, wechatProperties.getAppId(), wechatProperties.getAppSecret());
    }
}
